﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace SMSPanasonic.Business
{
    public partial class pa_Model
    {
    	#region public properties
		public string ModelCode { get; set; }
		public string CateCode { get; set; }
		public int WarrantyPeriod { get; set; }
		public string PeriodType { get; set; }
		public string Description { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public pa_Model(){ }

		public pa_Model(IDataReader rd)
		{
			this.ModelCode = rd["ModelCode"].ToString();
			this.CateCode = rd["CateCode"].ToString();
			this.WarrantyPeriod = (int)rd["WarrantyPeriod"];
			this.PeriodType = rd["PeriodType"].ToString();
			this.Description = rd["Description"].ToString();
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region static method
        public static pa_Model GetOne(string modelCode)
        {
            using (SqlDataReader rd = SqlHelper.ExecuteReader(AppConfig.ConnectionString, CommandType.StoredProcedure,
                        "usp_pa_Model_GetOne", new SqlParameter("@ModelCode", modelCode)))
            {
                while (rd.Read())
                {
                    return new pa_Model(rd);
                }
                return null;
            }
        }

        public static bool CheckExists(string modelCode)
        {
            return GetOne(modelCode) != null;
        }

        public static DateTime GetExpiredDate(string modelCode, DateTime registerDate)
        {
            try
            {
                return registerDate.AddMonths(pa_Model.GetOne(modelCode).WarrantyPeriod).GetLastDayOfMonth();
            }
            catch
            {
                return registerDate;
            }
        }
		#endregion
	}
}
